package com.aniu.smslisten.util;

import android.app.AlarmManager;
import android.os.SystemClock;

import java.util.Calendar;

public class ServiceTimer {
    private static final int START_HOUR = 7;
    private static final int END_HOUR = 22;

    public static boolean shouldNotRun() {
        int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        return currentHour < START_HOUR || currentHour > END_HOUR;
    }

    public static long nextAlarmTime(long currentTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(currentTime);

        int currentHour = cal.get(Calendar.HOUR_OF_DAY);
        if (currentHour < ServiceTimer.START_HOUR || currentHour > ServiceTimer.END_HOUR) {
            // 如果当前时间不在服务运行范围内，设置为下一天的 START_HOUR
            if (currentHour >= ServiceTimer.END_HOUR) {
                cal.add(Calendar.DAY_OF_YEAR, 1);
            }
            cal.set(Calendar.HOUR_OF_DAY, ServiceTimer.START_HOUR);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            return cal.getTimeInMillis();
        } else {
            // 如果当前时间在服务运行范围内，设置为当前时间的 15 分钟后
            return currentTime + AlarmManager.INTERVAL_FIFTEEN_MINUTES;
        }
    }
}